//
//  RxFeedbackTest1ViewController.swift
//  iOS-RxSwift-Tutorials_Example
//
//  Created by mengru.tian on 2020/9/24.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import RxFeedback
import RxCocoa

class RxFeedbackTest1ViewController: UIViewController {
    
    @IBOutlet weak var label: UILabel!  //显示数字的文本标签
    @IBOutlet weak var minus: UIButton! //减一按钮
    @IBOutlet weak var plus: UIButton! //加一按钮
    @IBOutlet weak var textView: UITextView!
    
    let disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "62.RxFeedback架构1：安装配置、基本用法"
        view.backgroundColor = .white
        
        test()
    }
    
    func test() {
        //RxRxFeedback的核心方法
        Driver.system(
            //初始状态
            initialState: State(id:0, content:""),
            //各个事件对状态的改变
            reduce: { (state, event) -> State in
                switch event {
                case .increment:
                    var result = state
                    result.id = result.id + 1
                    return result
                case .decrement:
                    var result = state
                    result.id = result.id - 1
                    return result
                case .response(let content):
                    var result = state
                    result.content = content
                    return result
                }
            },
            feedback:
                //UI反馈
                bind(self) { me, state in
                    //状态输出到页面控件上
                    let subscriptions = [
                        state.map{ "\($0.id)" }.drive(me.label!.rx.text),
                        state.map{ "\($0.content)" }.drive(me.textView!.rx.text)
                    ]
                    //将 UI 事件变成Event输入到反馈循环里面去
                    let events = [
                        me.plus!.rx.tap.map { Event.increment },
                        me.minus!.rx.tap.map { Event.decrement }
                    ]
                    return Bindings(subscriptions: subscriptions, events: events)
                },
                //非UI的自动反馈
                react(request: { $0.id }, effects: { id  in
                    return self.getContent(id: id)
                        .asSignal(onErrorRecover: { _ in .empty() })
                        .map(Event.response)
                })
            )
            .drive()
            .disposed(by: disposeBag)
    }
    
    //根据id获取对应数据
    func getContent(id: Int) -> Observable<String> {
        print("正在请求数据......")
        let observable = Observable.just("这个是 id=\(id) 的新闻内容......")
        //延迟1秒模拟网络请求
        return observable.delay(1, scheduler: MainScheduler.instance)
    }

}

extension RxFeedbackTest1ViewController {
    //状态（就是一个Int类型的数字）
    //状态
    struct State {
        var id: Int //id数字
        var content: String //当前id对应的内容
    }
     
    //事件
    enum Event {
        case increment //加一
        case decrement //减一
        case response(String) //获取到内容
    }
}
